<?php

namespace App\Http\Requests;

use App\Http\Requests\BaseFormRequest;

class UserRequest extends BaseFormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    public function scenes()
    {
		$id = $this->input("userid");
        return [
            'store'=>[
				'username' => 'required|unique:sy_user|max:255',
				'userphone' => ['required', config('site.phone'), 'unique:userinfo'],
				'userpwd' => 'required|confirmed|min:6',
			],
			'update'=>[
				'username' => 'required|unique:sy_user,username,' . $id . ',userid|max:255',
				'userphone' => ['required', config('site.phone'), 'unique:userinfo,userphone,' . $id . ',userid'],
			]
        ];
    }
	
	public function messages()
    {
        return [
        'username.required' => '用户名称必须',
        'username.unique' => '用户名称已经存在',
        'username.max' => '用户名称不能超过255个字符',
        'userphone.required' => '用户手机号必须',
        'userphone.unique' => '用户手机号已经存在',
        'userphone.regex' => '用户手机号格式不正确',
        'userpwd.required' => '用户密码必须',
        'userpwd.confirmed' => '两次输入密码不一致',
        'userpwd.min' => '用户密码至少6个字符',
        'Co_ID.required' => '所属公司必须',
        'Co_ID.integer' => '所属公司无效',
        'Co_ID.exists' => '所属公司不存在',

		'scope.required' => '所属站点必须',
        'scope.integer' => '所属站点参数非法',
        'scope.exists' => '所属站点不存在',
		
        ];
    }
	
	public function withValidator($validator)
	{

		$validator->sometimes('Co_ID', 'required|integer|exists:t_companycnfo', function($input){
            return true;
        });

        $validator->sometimes('scope', 'required|integer|exists:sy_area,AreaId', function($input){
            $roles = array_except(config('auth.roles'), 'default');
            $flag = false;
            foreach($input->role as $role) {
                if(in_array($role, $roles)){
                    $flag = true;
                    break;
                }
            }
            return !$flag;
        });
	}
}
